﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;

namespace WoWGoldWatch
{
    public partial class Form1 : Form
    {
        GoldFileLoader _pLoader;

        bool _loadCompleted = false;

        public Form1()
        {
            InitializeComponent();
        }

        [DefaultValue(@"D:\games\wow\gold")]
        public string WatchDirectory
        {
            get;
            set;
        }

        public GoldFileLoader Loader
        {
            get
            {
                if (_pLoader == null)
                {
                    _pLoader = new GoldFileLoader(WatchDirectory);
                }

                return _pLoader;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxPeriod.SelectedIndex = 0;

            LoadGold();

            FileSystemWatcher fsw = new FileSystemWatcher(Loader.Dir.FullName, "*.gold.txt");
            fsw.EnableRaisingEvents = true;
            fsw.Deleted += (s1, e1) => { LoadGold(); };
            fsw.Changed += (s1, e1) => { LoadGold(); };
            fsw.Created += (s1, e1) => { LoadGold(); };
            fsw.Renamed += (s1, e1) => { LoadGold(); };

            _loadCompleted = true;
        }

        Dictionary<string, Series> _dcSeries = new Dictionary<string, Series>();

        public delegate void LoadGoldCallback();

        void LoadGold()
        {
            try
            {
                DateTime dtFirstEntry = DateTime.Now;
                switch (comboBoxPeriod.SelectedIndex)
                {
                    case 0:
                        dtFirstEntry = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));
                        break;
                    case 1:
                        dtFirstEntry = DateTime.Now.Subtract(new TimeSpan(14, 0, 0, 0));
                        break;
                    case 2:
                        dtFirstEntry = DateTime.Now.Subtract(new TimeSpan(31, 0, 0, 0));
                        break;
                    default:
                        dtFirstEntry = DateTime.MinValue;
                        break;
                }

                if (dataGridView.InvokeRequired)
                {
                    LoadGoldCallback d = new LoadGoldCallback(LoadGold);
                    Invoke(d);
                }
                else
                {
                    _loadCompleted = false;

                    dataGridView.Rows.Clear();
                    _dcSeries.Clear();
                    chart.Series.Clear();

                    foreach (GoldFile gf in Loader.GoldFiles)
                    {
                        dataGridView.Rows.Add(gf.ServerName, gf.Gold, gf.LastUpdate.ToLocalTime().ToString());

                        // chart
                        Series s = chart.Series.Add(gf.ServerName);
                        s.ChartType = SeriesChartType.Line;
                        s.BorderWidth = 2;
                        _dcSeries.Add(gf.ServerName, s);

                        foreach (GoldFileEntry e in gf.Entries)
                        {
                            if (e.Date.ToLocalTime() >= dtFirstEntry)
                            {
                                s.Points.AddXY(e.Date, e.Gold);
                            }
                        }
                    }

                    _loadCompleted = true;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                throw;
            }
        }

        private void dataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (!_loadCompleted)
            {
                return;
            }

            string serverName = (string)dataGridView.Rows[e.RowIndex].Cells[0].Value;

            if (serverName != null)
            {
                _dcSeries[serverName].BorderWidth = 5;
            }
        }

        private void dataGridView_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (!_loadCompleted)
            {
                return;
            }

            string serverName = (string)dataGridView.Rows[e.RowIndex].Cells[0].Value;

            if (serverName != null)
            {
                _dcSeries[serverName].BorderWidth = 2;
            }
        }

        private void comboBoxPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGold();
        }
    }
}
